 <template>
 	<view class="flex_ZC">
 		<!-- #ifdef H5 -->
 		<uNavbar title="快捷买单"> </uNavbar>
 		<!-- #endif -->

 		<view class="business flex_dq">
 			<image :src="info.cover" mode="aspectFill"></image>
 			<view class="business_name flex_Z">
 				<text class="f30 c3 fw5">{{info.name}}</text>
 				<text class="f23 c6  " style="margin-top: 10rpx;">{{info.pay_code_name}}</text>
 				<text class="integral">{{info.quick_integral_proportion}}%More值</text>
 			</view>
 		</view>
 		<view class="custom flex_Z">
 			<text class="f28 c3">买单金额</text>
 			<view class="custom_input flex_dq">
 				<text class="symbol">￥</text>
 				<input type="digit" v-model.trim="pricess" placeholder="请输入买单金额" />
 			</view>
 		</view>
 		<view class="coupon flex_ld" v-if=" Number(usermoney)>=0.01">
 			<view class="coupon_l flex_dq">
 				<image :src="baseUrl +'/static/icon/i_dk.png'" mode="aspectFill"></image>
 				<text class="f30 c3">使用More券抵扣</text>
 			</view>
 			<view class="coupon_r flex_dq" @click="changeCoupons">
 				<text class="f28 c3">{{usermoney}}</text>
 				<image v-if="!isMoney" class="checkboxHead" :src="baseUrl +'/static/icon/i_wxz.png'" mode="aspectFill">
 				</image>
 				<image v-else class="checkboxHead" :src="baseUrl +'/static/icon/i_xz.png'" mode="aspectFill"></image>
 			</view>
 		</view>
 		<view class="coupon flex_ld" v-if=" Number(usermoney)<0.01">
 			<view class="coupon_l flex_dq">
 				<image :src="baseUrl +'/static/icon/i_dk.png'" mode="aspectFill"></image>
 				<text class="f30 c3">More券余额不足</text>
 			</view>
 			<view class="coupon_r flex_dq">
 				<text class="f28 c3">{{usermoney}}</text>

 			</view>
 		</view>
 		<view class="btn_Pay flex_c" @click="isPlaceOrder">
 			<text class="f36 c0 ">去支付 {{totalPrice}}</text>
 		</view>
 		<u-popup :show="tipShow" mode="center" @close="tipShow = false" closeable bgColor="transparent">
 			<view class="prompt flex_ZC">
 				<text class="f36">提示</text>
 				<text class="prompt_tit">尚未设置More交易密码？</text>
 				<view class="prompt_ntm flex_ld_a">
 					<text @click="tipShow = false">取消</text>
 					<text @click="goPayPwd">去设置</text>
 				</view>
 			</view>
 		</u-popup>
 		<u-popup :show="RealShow" mode="center" @close="RealShow = false" closeable bgColor="transparent">
 			<view class="prompt flex_ZC">
 				<text class="f36">提示</text>
 				<text class="prompt_tit">{{tipstxt}}</text>
 				<view class="prompt_ntm flex_ld_a">
 					<text @click="RealShow = false">取消</text>
 					<text @click="goRealname">去实名</text>
 				</view>
 			</view>
 		</u-popup>


 		<u-popup :show="tipShowTwo" mode="center" @close="tipShowTwo = false" closeable bgColor="transparent">
 			<view class="prompt flex_ZC">
 				<image class="gghimg" :src="baseUrl+  '/static/image/gzhewm.jpg'" mode="widthFix"
 					:show-menu-by-longpress="true" @longpress="handleLongPress"></image>
 				<text class="prompt_tit">长按图片二维码扫描</text>
 				<text class="prompt_tit fw7">请使用微信公众号支付</text>
 			</view>
 		</u-popup>
 		<u-keyboard ref="uKeyboard" dotDisabled mode="number" bgColor="transparent" closeOnClickOverlay
 			:tooltip="false" :show="keyboardShow" @close="isClose" @change="changePwd" @backspace="backspace">
 			<view class="keyboardss flex_ZC">
 				<u-code-input dot space="26" :maxlength="6" v-model="securityPwd"></u-code-input>
 			</view>
 		</u-keyboard>
 	</view>
 </template>

 <script>
 	import {
 		miniAppLogin,
 		wechartLogin
 	} from "../api/login.js"
 	import {
 		myuser
 	} from "../api/user.js"
 	import {
 		detail,
 		goodsdetail,
 		createorder,
 		seller
 	} from "../api/local.js"
 	export default {
 		data() {
 			return {
 				baseUrl: this.$imageBaseUrl,
 				pricess: '',
 				id: "",
 				info: {},
 				isMoney: false,
 				payPasword: "",
 				keyboardShow: false,
 				securityPwd: '',
 				tipShow: false,
 				tipShowTwo: false,
 				user: {

 				},
 				usermoney: "",
 				code: "",
 				isfrom: "",
 				pay_code: "",
 				secreShow: false,
 				ttcode: "",
 				RealShow: false,
 				tipstxt: ""
 			}
 		},
 		computed: {
 			// 计算总价
 			totalPrice() {
 				if (this.isMoney) {
 					if (Number(this.user.money) - Number(this.pricess) >= 0) {
 						return "0.01"
 					} else {
 						return (Number(this.pricess) - (this.user.money)).toFixed(2);
 					}
 				} else {
 					return Number(this.pricess).toFixed(2);
 				}
 			},
 		},
 		watch: {
 			securityPwd(newValue) {
 				console.log(newValue.length);
 				if (newValue.length >= 6) {
 					this.keyboardShow = false
 					this.isBuyNow()
 				}
 			}
 		},
 		onShow() {
 			uni.removeStorageSync("is_pay")
 			// 扫码支付宝小程序快捷支付
 			// #ifdef MP-ALIPAY
 			const options = my.getLaunchOptionsSync();
 			if (options.query) {
 				let qrCode = options.query.qrCode
 				let page = qrCode.split('?')[1].split('&')[0]
 				if (!uni.getStorageSync("token")) {
 					uni.setStorageSync("pay_code", page.split('=')[1])
 					uni.setStorageSync("is_payPage", 1)
 					uni.setStorageSync("quick_pay", `/pages/pay?${page}`)
 				} else {
 					uni.removeStorageSync("quick_pay")
 					uni.removeStorageSync("pay_code")
 				}
 				this.code = page.split('=')[1]
 			}
 			console.log(this.code, 'this.codeonLaunch参数')
 			// #endif
 		},
 		onLoad(e) {
 			console.log(e, 'onload参数')
 			if (e && e.scene) {
 				var scene = e.scene;
 				// 使用 decodeURIComponent 解码
 				var decodedScene = decodeURIComponent(scene);
 				// 将解码后的字符串转换为对象
 				var parsedScene = JSON.parse('{"' + decodedScene.replace(/"/g, '\\"').replace(/=/g, '":"') + '"}');
 				console.log(parsedScene, "编译大多少")
 				this.code = parsedScene.pay_code
 			}
 			// #ifdef H5
 			if (e && e.pay_code) {
 				this.code = e.pay_code
 			}
 			if (!uni.getStorageSync("token")) {

 				uni.setStorageSync("pay_code", e.pay_code)
 				uni.setStorageSync("is_payPage", 1)
 				uni.setStorageSync("quick_pay", `/pages/pay?pay_code=${e.pay_code}`)
 			} else {
 				uni.removeStorageSync("quick_pay")
 				uni.removeStorageSync("pay_code")
 				uni.removeStorageSync("is_payPage")
 			}
 			// #endif

 			console.log(this.code, 'this.codeonload参数')
 			setTimeout(() => {
 				this.getinfo()
 				this.getEnv()
 				this.userInfo()
 			}, 500)

 		},

 		methods: {
 			goRealname() {
 				this.RealShow = false
 				uni.navigateTo({
 					url: "/pageUser/myuser/realname"
 				})
 			},
 			isWechat() {
 				var ua = navigator.userAgent.toLowerCase()
 				console.log(ua);

 				var isWXWork = ua.match(/wxwork/i) == 'wxwork'

 				var isWeixin = !isWXWork && ua.match(/MicroMessenger/i) == 'micromessenger'
 				return isWeixin

 			},
 			getUrlCode() {
 				// 截取url中的code方法
 				var url = location.search;
 				console.log('url------', url);
 				var theRequest = new Object();
 				if (url.indexOf("?") != -1) {
 					var str = url.substr(1);
 					var strs = str.split("&");
 					for (var i = 0; i < strs.length; i++) {
 						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
 					}
 				}
 				console.log('theRequest-----------', theRequest);
 				return theRequest;
 			},
 			removeCodeAndStateFromUrl() {
 				var url = window.location.href;
 				var baseUrl = url.split('?')[0];
 				var queryString = url.split('?')[1];

 				if (queryString) {
 					var params = queryString.split('&');
 					var filteredParams = params.filter(param => {
 						return !param.startsWith('code=') && !param.startsWith('state=');
 					});

 					if (filteredParams.length > 0) {
 						var newQueryString = filteredParams.join('&');
 						var newUrl = baseUrl + '?' + newQueryString;
 					} else {
 						var newUrl = baseUrl;
 					}
 				} else {
 					var newUrl = baseUrl;

 				}
 				console.log("newurl", newUrl)
 				window.history.replaceState({}, document.title, newUrl + "?pageLogin/login/login");
 			},
 			wxgzhLogin() {
 				if (this.ttcode == null || this.ttcode === "") {
 					// 如果没有code，跳转到微信授权页面
 					const appId = 'wx21cc703fbbd18c6a'; // 公众号的appid
 					const redirectUri = encodeURIComponent(window.location.href);
 					console.log("重定向地址：ssssss", redirectUri)
 					const authUrl =
 						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
 					window.location.href = authUrl;
 				} else {
 					this.ttcode = code
 					//此处调用后端提供的接口，传入获取到的code换取access_token
 					this.getOpenId()
 				}
 			},
 			getWXCode() {
 				let code = this.getUrlCode().code; //是否存在code
 				console.log('-----------', code);
 				this.ttcode = code
 				console.log('检查--', this.ttcode);
 				if (this.ttcode) {
 					this.getOpenId()
 				} else {
 					this.wxgzhLogin()
 				}
 			},
 			getOpenId() {
 				let data = {
 					code: this.ttcode,
 					invite_code: "",
 					seller_id: uni.getStorageSync("seller_id")
 				}
 				wechartLogin(data).then(res => {
 					// uni.$u.toast(res.msg);
 					console.log('登录-----------', res)
 					if (res.code == 1) {
 						console.log(res);

 						uni.setStorageSync('token', res.data.token)
 						if (uni.getStorageSync("quick_pay")) {
 							let url = uni.getStorageSync("quick_pay")
 							setTimeout(function() {
 								uni.redirectTo({
 									url: url
 								})
 							}, 1500);
 						} else {
 							setTimeout(function() {
 								uni.switchTab({
 									url: '/pages/index/cloudCatIndex'
 								})
 							}, 1500);
 						}



 					} else {
 						this.removeCodeAndStateFromUrl();
 						this.getWXCode()

 					}
 				})
 			},
 			goPayPwd() {
 				uni.navigateTo({
 					url: "/pageUser/myuser/amendsafepassword"
 				})
 			},
 			getEnv() {
 				let isfrom;
 				// 小程序环境判断
 				// #ifdef MP-WEIXIN
 				try {
 					const systemInfo = uni.getSystemInfoSync();
 					console.log(systemInfo)
 					if (systemInfo.platform === 'devtools') {
 						isfrom = '浏览器开发者工具';
 					} else if (systemInfo.app) {
 						const app = systemInfo.app.toLowerCase();
 						if (app.includes('alipay')) {
 							isfrom = '支付宝小程序';
 						} else if (app.includes('micromessenger')) {
 							isfrom = '微信小程序';
 						}
 					}
 				} catch (error) {
 					// 如果 uni.getSystemInfoSync 报错，说明可能不是小程序环境
 				}
 				// #endif
 				// #ifdef H5
 				if (!isfrom) {
 					const ua = navigator.userAgent.toLowerCase();
 					if (ua.match(/Alipay/i)) {
 						isfrom = '支付宝浏览器';
 					} else if (ua.match(/MicroMessenger/i)) {
 						isfrom = '微信内置浏览器';
 					} else {
 						isfrom = '普通浏览器';
 					}
 				}
 				// #endif

 				// H5环境判断


 				// 跳转逻辑
 				if (isfrom === "微信小程序" || isfrom === "微信内置浏览器") {
 					// 这里可以添加微信相关的跳转逻辑
 				} else {

 				}
 				this.isfrom = isfrom
 				console.log(this.isfrom, "环境")
 				return isfrom;
 			},
 			getinfo() {
 				console.log(this.code, '123456');
 				seller({
 					code: this.code
 				}).then(res => {
 					uni.setStorageSync("seller_id", res.data.id)
 					if (uni.getStorageSync("red_id")) {
 						uni.removeStorageSync("red_id")
 						uni.removeStorageSync("inviteCode")
 					}
 					this.info = res.data
 				})
 			},
 			isBuyNow() {
 				let token = uni.getStorageSync('token');

 				if (token) {
 					let data = {
 						type: '2',
 						is_money: this.isMoney ? 1 : 0,
 						goods_id: this.id,
 						pay_password: this.securityPwd,
 						pay_price: this.pricess,
 						seller_id: this.info.id,
 						pay_code_id: this.info.pay_code_id,
 						number: 1,
 						mask: 1
 					}
 					console.log(data);
 					createorder(data).then(res => {
 						console.log('详情', res)
 						if (res.code == 1) {
 							this.payShow = false
 							if (res.data.status == 1) {
 								uni.navigateTo({
 									url: '/pagesMy/cloudCat/paymentCenter?type=2' + '&id=' + res.data.id +
 										'&totalPrice=' + this.totalPrice
 								})
 								return
 							}
 							uni.$u.toast(res.msg)
 							if (this.isMoney) {
 								setTimeout(function() {
 									uni.redirectTo({
 										url: '/pagesMy/cloudCat/paySuccess?type=2'
 									})
 								}, 1200);
 							} else {
 								uni.navigateTo({
 									url: '/pagesMy/cloudCat/paymentCenter?type=2' + '&id=' + res.data.id +
 										'&totalPrice=' + this.totalPrice
 								})
 							}

 						} else if (res.code == 1001) {
 							this.tipstxt = res.msg
 							this.RealShow = true
 							// 清空密码
 						} else {
 							this.securityPwd = ''
 						}
 					})
 				} else {
 					uni.navigateTo({
 						url: '/pageLogin/login/login'
 					})
 				}
 			},
 			isPlaceOrder() {
 				if (!this.pricess) {
 					uni.$u.toast("请输入价格")
 					return
 				}
 				if (this.pricess == "0.00") {
 					uni.$u.toast("请输入价格")
 					return
 				}
 				// #ifdef H5 || MP-ALIPAY
 				if (this.isMoney) {
 					this.isBuyNow()
 				} else {
 					this.isBuyNow()
 				}

 				// #endif
 				// #ifdef MP-WEIXIN
 				this.tipShowTwo = true

 				// if (this.isMoney) {
 				// 	if (this.payPasword) {
 				// 		this.keyboardShow = true
 				// 		this.payShow = false
 				// 	} else {
 				// 		this.payShow = false
 				// 		this.tipShow = true
 				// 	}
 				// } else {
 				// 	this.isBuyNow()

 				// }

 				// #endif

 			},
 			isClose() {
 				this.keyboardShow = false
 				this.payShow = true
 			},
 			// 输入密码
 			changePwd(val) {
 				if (this.securityPwd.length < 6) {
 					this.securityPwd += val;
 					// console.log(this.securityPwd);
 				} else {
 					console.log('超出不累计');
 				}
 			},
 			// 退格键被点击
 			backspace() {
 				// 删除value的最后一个字符
 				if (this.securityPwd.length) this.securityPwd = this.securityPwd.substr(0, this.securityPwd.length - 1);
 				console.log(this.securityPwd);
 			},
 			userInfo() {
 				myuser().then(res => {
 					console.log('个人信息', res)
 					if (res.code == 1) {
 						this.user = res.data
 						this.usermoney = res.data.money
 						this.secreShow = res.data.secret_free
 						console.log(this.usermoney)
 						this.payPasword = res.data.is_pay_pasword
 					} else if (res.code == 401) {
 						if (this.isfrom == "微信内置浏览器") {
 							this.getWXCode()
 						} else {
 							uni.$u.toast("请重新登录")
 							setTimeout(() => {
 								uni.navigateTo({
 									url: '/pageLogin/login/login'
 								})
 							}, 1500)
 						}
 					}
 				})
 			},
 			// 免密支付
 			handleLongPress(e) {
 				console.log(e);
 			},
 			changeCoupons() {
 				this.isMoney = !this.isMoney
 			},
 			getdetail() {
 				detail({
 					id: this.id
 				}).then(res => {
 					this.info = res.data
 					console.log()
 				})
 			},
 			completePay() {
 				console.log(11111);
 			}
 		},
 	}
 </script>

 <style>
 	page {
 		background: #ffffff;
 	}
 </style>

 <style scoped lang="scss">
 	.business {
 		width: 750rpx;
 		padding: 60rpx 60rpx 40rpx;
 		/* margin-top: 50rpx; */
 		background: #f7f7f7;
 	}

 	.business>image {
 		width: 112rpx;
 		height: 112rpx;
 		border-radius: 20rpx;
 	}

 	.business_name {
 		margin-left: 20rpx;
 	}

 	.integral {
 		font-size: 24rpx;
 		color: #ffffff;
 		width: 140rpx;
 		height: 32rpx;
 		line-height: 32rpx;
 		text-align: center;
 		background: linear-gradient(90deg, #FA2222 2%, #FFC400 98%);
 		border-radius: 38rpx;
 		margin-top: 14rpx;
 	}

 	.custom {
 		width: 660rpx;
 		margin-top: 62rpx;
 	}

 	.custom_input {
 		/* background: pink; */
 		margin-top: 54rpx;
 		padding-bottom: 10rpx;
 		border-bottom: 1rpx solid #F2F2F2;
 	}

 	.symbol {
 		font-size: 52rpx;
 		color: #333333;
 		font-weight: 500;
 		margin-right: 20rpx;
 	}

 	.custom_input>input {
 		font-size: 36rpx;
 		color: #333;
 	}

 	.btn_Pay {
 		width: 690rpx;
 		height: 80rpx;
 		margin-top: 254rpx;
 		background: linear-gradient(90deg, #FA2222 2%, #FFC400 98%);
 		border-radius: 138rpx 138rpx 138rpx 138rpx;
 	}

 	.prompt {
 		width: 600rpx;
 		background: #ffffff;
 		margin: 0 auto;
 		border-radius: 20rpx;
 		padding-top: 10rpx;
 		padding-bottom: 40rpx;
 	}

 	.prompt>text {
 		margin-top: 40rpx;
 	}

 	.prompt_tit {
 		margin: 0 30rpx;
 		font-size: 28rpx;
 		color: #666666;
 		text-overflow: ellipsis;
 		display: -webkit-box;
 		-webkit-box-orient: vertical;
 		-webkit-line-clamp: 2;
 		overflow: hidden;
 	}

 	.prompt_ntm {
 		width: 100%;
 		font-size: 30rpx;
 		margin-top: 50rpx;
 	}

 	.prompt_ntm>text:nth-child(1) {
 		width: 200rpx;
 		height: 72rpx;
 		display: flex;
 			justify-content: center;
 			align-items: center;
 		border: 1rpx solid #FFC300;
 		border-radius: 40rpx;
 		color: #FFC300;
 	}

 	.prompt_ntm>text:nth-child(2) {
 		width: 200rpx;
 		height: 72rpx;
 		display: flex;
 		justify-content: center;
 		align-items: center;
 		background: #FFC300;
 		border-radius: 40rpx;
 		color: #ffffff;
 	}

 	.coupon {
 		width: 690rpx;
 		background: #FFFFFF;
 		border-radius: 24rpx;
 		padding: 24rpx 24rpx;
 	}

 	.coupon_l>image {
 		width: 36rpx;
 		height: 36rpx;
 		margin-right: 18rpx;
 	}

 	.checkboxHead {
 		width: 36rpx;
 		height: 36rpx;
 		margin-left: 12rpx;
 	}

 	.keyboardss {
 		width: 750rpx;
 		padding: 50rpx 0;
 		background: #ffffff;
 	}

 	.gghimg {
 		width: 300rpx;
 		height: 300rpx;
 		margin-top: 30rpx;
 	}
 </style>